package com.sh.personnel.security.util;

import com.sh.personnel.security.ISecurityUser;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * security自定义工具类
 * @author 林思浩
 * @date 2020/03/13 13:40
 */
public class SecurityUtil {

    public static ISecurityUser getUser() {
        try {
            return (ISecurityUser) getAuthentication().getPrincipal();
        } catch (Exception e) {
            throw new InsufficientAuthenticationException("登录已过期，请重新登录!");
        }
    }

    /**
     * 获取当前上下文的Authentication
     * @return
     */
    private static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

}
